<?php

defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
 
$tpl=new TemplatePower("skin/sk_module.htm");
$tpl->prepare();
$id=intval($_GET['id']);

$tpl->assignGlobal("title","Quản lý modules");
$tpl->assignGlobal("pathpage","<a href='?page=sr_module'>Danh sách modules</a>");
if(!$_GET['code'] or $_GET['code']=='00'){
	show_list();
}
if($_GET['code']=='01'){
	$tpl->newBlock("addnewitem");
}
if($_GET['code']=='02'){
	
	$name=compile_post("name");
	$active=intval(compile_post("active"));
	$thu_tu=intval(compile_post("thu_tu"));
	$gia_tri=compile_post("gia_tri");
	
	
	if($name!='' && $gia_tri!=''){
		$a=array();
		$a['name']		=$name;
		$a['active']	=$active;
		$a['thu_tu']	=$thu_tu;
		$a['gia_tri']	=$gia_tri;
		
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO module (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		message("Th&#234;m m&#7899;i Nh&#243;m th&#224;nh c&#244;ng !");
	}else{
		message("Không đủ dữ liệu đầu vào !");
	}
	
	show_list();
}
if($_GET['code']=='03'){
	$tpl->newBlock("edititem");
	$sql="SELECT * FROM module WHERE id_module=$id";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		$tpl->assign("gia_tri",$rs['gia_tri']);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		$tpl->assign("id_module",$rs['id_module']);
		if($rs['active']=='1') $tpl->assign("active","checked");
	}
}
if($_GET['code']=='04'){
	$a=array();
	$name=compile_post("name");
	$active=intval(compile_post("active"));
	$thu_tu=intval(compile_post("thu_tu"));
	$gia_tri=compile_post("gia_tri");
	
	if($name!='' && $gia_tri!=''){
		$a=array();
		$a['name']		=$name;
		$a['active']	=$active;
		$a['thu_tu']	=$thu_tu;
		$a['gia_tri']	=$gia_tri;
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE module SET ".$b." WHERE id_module=".$id;
		$DB->query($sql);
		message("S&#7917;a th&#224;nh c&#244;ng !");
	}else{
		message("Không đủ dữ liệu đầu vào !");
	}
	show_list();
}

if($_GET['code']=='05'){
	$sql="DELETE FROM module WHERE id_module=$id";
	$db=$DB->query($sql);
	if($db) message("Đã xóa xong !");
	else message("Lỗi không xóa được !");
	show_list();
}

if($_GET['code']=='06'){
	$sql="SELECT * FROM module  ORDER BY thu_tu DESC, name ASC";
	$c=$DB->query($sql);
	$a=array();
	while ($d=mysql_fetch_array($c))
	{
		$active=intval(compile_post('active_'.$d['id_module']));
		if ($active!=$d['active'])
		{
			$a['active']=$active;
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE module SET ".$b." WHERE id_module='".$d['id_module']."'";
			$ok=$DB->query($sql);
		}
	}
	if($ok) message("L&#432;u th&#224;nh c&#244;ng !");
	show_list();
}

function show_list(){
	global $DB, $tpl;
	$tpl->newBlock("cell_list");
	$sql="SELECT * FROM module  ORDER BY thu_tu DESC, name ASC";
	$db=$DB->query($sql);
	while($rs=mysql_fetch_array($db)){
		$tpl->newBlock("list_module");
		$tpl->assign("name",$rs['name']);
		$tpl->assign("gia_tri",$rs['gia_tri']);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id_module",$rs['id_module']);
	}
}

$tpl->printToScreen();
?>